;(function () {
	
	'use strict';

	// iPad and iPod detection	 iPad和iPod检测
	var isiPad = function(){
		return (navigator.platform.indexOf("iPad") != -1);
	};

	var isiPhone = function(){
	    return (
			(navigator.platform.indexOf("iPhone") != -1) || 
			(navigator.platform.indexOf("iPod") != -1)
	    );
	};

	// OffCanvass 非画布
	var offCanvass = function() {
		$('body').on('click', '.js-fh5co-menu-btn, .js-fh5co-offcanvass-close', function(){
			$('#fh5co-offcanvass').toggleClass('fh5co-awake');
		});
	};

	// Click outside of offcanvass 在非画布外单击
	var mobileMenuOutsideClick = function() {
		$(document).click(function (e) {
	    var container = $("#fh5co-offcanvass, .js-fh5co-menu-btn");
	    if (!container.is(e.target) && container.has(e.target).length === 0) {
	    	if ( $('#fh5co-offcanvass').hasClass('fh5co-awake') ) {
	    		$('#fh5co-offcanvass').removeClass('fh5co-awake');
	    	}
	    }
		});

		$(window).scroll(function(){
			if ( $(window).scrollTop() > 500 ) {
				if ( $('#fh5co-offcanvass').hasClass('fh5co-awake') ) {
		    		$('#fh5co-offcanvass').removeClass('fh5co-awake');
		    	}
	    	}
		});
	};

	// Magnific Popup 放大弹出窗口
	
	var magnifPopup = function() {
		$('.image-popup').magnificPopup({
			type: 'image',
			removalDelay: 300,
			mainClass: 'mfp-with-zoom',
			titleSrc: 'title',
			gallery:{
				enabled:true
			},
			zoom: {
				enabled: true, // By default it's false, so don't forget to enable it

				duration: 300, // duration of the effect, in milliseconds
				easing: 'ease-in-out', // CSS transition easing function

				// The "opener" function should return the element from which popup will be zoomed in
				// and to which popup will be scaled down
				// By defailt it looks for an image tag:
				opener: function(openerElement) {
				// openerElement is the element on which popup was initialized, in this case its <a> tag
				// you don't need to add "opener" option if this code matches your needs, it's defailt one.
				return openerElement.is('img') ? openerElement : openerElement.find('img');
				}
			}
		});
	};



	var animateBoxWayPoint = function() {

		if ($('.animate-box').length > 0) {
			$('.animate-box').waypoint( function( direction ) {

				if( direction === 'down' && !$(this).hasClass('animated') ) {
					$(this.element).addClass('bounceIn animated');
				}

			} , { offset: '75%' } );
		}

	};

	
	function getQueryString1(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if ( r != null ){
            return decodeURIComponent(r[2]);
        }else{
            return null;
        }
    }
	
	//文档高度
	function getDocumentTop() {
		var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
		if (document.body) {
			bodyScrollTop = document.body.scrollTop;
		}
		if (document.documentElement) {
			documentScrollTop = document.documentElement.scrollTop;
		}
		scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
		return scrollTop;
	}

	//可视窗口高度
	function getWindowHeight() {
		var windowHeight = 0;
		if (document.compatMode == "CSS1Compat") {
			windowHeight = document.documentElement.clientHeight;
		} else {
			windowHeight = document.body.clientHeight;
		}
		return windowHeight;
	}

	//滚动条滚动高度
	function getScrollHeight() {
		var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
		if (document.body) {
			bodyScrollHeight = document.body.scrollHeight;
		}

		if (document.documentElement) {
			documentScrollHeight = document.documentElement.scrollHeight;
		}
		scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
		return scrollHeight;
	}


/*
当滚动条滑动，触发事件，判断是否到达最底部
然后调用ajax处理函数异步加载数据
*/


	$(function(){

		var nStart =18;
		var sum=$("works_sum").val();
        var keyword=getQueryString1('keyword');
        var sort=getQueryString1('sort');
        var worksclass=getQueryString1('worksclass');
		window.onscroll=function(){
			if (getScrollHeight() == getWindowHeight() + getDocumentTop()) {
			$.ajax({
                type: "post",
                data:  { "start":nStart,"keyword":keyword,"sort":sort,"worksclass":worksclass },
                url: "/reboot/getMoreWorks",
                beforeSend: function () {
                    $("#loadingMore").addClass('show');
				},
				success: function (res) {
					var conter=$('#fh5co-board')[0];
					var hts=[];
					console.log(res['result']);
					for(var i=0;i<res['result'].length;i++){
						hts[i-1]=$('<div id="wors_id_'+res['result'][i]['works_id']+'" class="item thumbnail"><a  class="image-popup fh5co-board-img" title="'+res['result'][i]['works_name']+'">'+
							'<img src="'+res['result'][i]['cover']+'" class="cover" alt="Free HTML5 Bootstrap template"></a>'+
							'<div class="caption"><h4>'+res['result'][i]['works_name']+'</h4><p class="small text-muted">'+res['result'][i]['works_class']+'</p>'+
							'<ul class="list-inline text-muted"><li><i class="fa fa-heart"></i> '+res['result'][i]['thumbs_up']+'</li>'+
							// '<li><i class="fa fa-external-link"></i> 5566</li>'+							
							'<li><i class="fa fa-compass"></i> '+res['result'][i]['browsetimes']+'</li></ul><hr/>'+
							'<div class="media " class=""><div class="media-left">'+
							'<a href="#"><img class="userAvatar img-circle media-object" src="'+res['result'][i]['user_avatar']+'" alt="'+res['result'][i]['user_nicename']+'"></a>'+
							'</div>'+
							'<div class="media-body">'+
							'<p>'+res['result'][i]['user_nicename']+'<a  href="javascript:void();" class="pull-right text-right">'+
							'<i class="fa fa-heart-o fa-2x text-muted"></i></a>'+
							'</p><p class="small text-muted">'+res['result'][i]['user_realname']+'</p></div></div></div></div>')[0];
							// $("#wors_id_"+res['result'][i]['works_id']+" .fh5co-board-img").height($(".item img").width()*res['result'][i]['coverScale'])	
						}
					salvattore.append_elements(conter, hts);
					$("#loadingMore").removeClass('show');
					$("#loadingMore").addClass('hidden');
				}
			})
			nStart +=12;
		}
	}
	});


}());



